package com.yueqian.epidemic.mapper;

import com.yueqian.epidemic.beans.EpidemicDetailInfo;
import com.yueqian.epidemic.beans.EpidemicInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface EpidemicMapper {
    /**
     * 保存一条记录
     * @param epidemicInfo
     * @return
     */
    @Insert(value ="INSERT INTO epidemics(province_id,data_year,data_month,data_day,affirmed,suspected,isolated,dead, cured,user_id,input_date)VALUES(#{provinceId},#{dataYear},#{dataMonth},#{dataDay},#{affirmed},#{suspected},#{isolated},#{dead},#{cured},#{userId},#{inputDate})")
    int saveInfo(EpidemicInfo epidemicInfo);


    @Select(value = "SELECT e1.`province_id`,temp.province_name,e1.`data_year`,e1.`data_month`,e1.`data_day`, " +
            "\ttemp.affirmed_total,temp.suspected_total,temp.isolated_total,temp.cured_total,temp.dead_total, " +
            "\te1.`affirmed`,e1.`suspected`,e1.`isolated`,e1.`cured`,e1.`dead` " +
            "FROM " +
            "\tepidemics e1 " +
            "RIGHT OUTER JOIN ( " +
            "\tSELECT " +
            "\t\te.`province_id`, " +
            "\t\tp.`province_name`, " +
            "\t\tSUM(e.`affirmed`) affirmed_total, " +
            "\t\tSUM(e.`suspected`) suspected_total, " +
            "\t\tSUM(e.`isolated`) isolated_total, " +
            "\t\tSUM(e.`cured`) cured_total, " +
            "\t\tSUM(e.`dead`) dead_total " +
            "\tFROM " +
            "\t\tepidemics e " +
            "\tRIGHT OUTER JOIN provinces p ON e.`province_id` = p.`province_id` " +
            "\tGROUP BY " +
            "\t\te.`province_id`, " +
            "\t\tp.`province_name` " +
            ") temp ON e1.`province_id` = temp.province_id " +
            "WHERE " +
            "\te1.`data_year` = #{year} " +
            "AND e1.`data_month` = #{month} " +
            "AND e1.`data_day` = #{day}")
    List<EpidemicDetailInfo> findLastestData(Map<String, Short> condition);
}
